package com.lyhengtongwl.zqsnews.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.entity.MyorderEntity;
import com.lyhengtongwl.zqsnews.utils.FormattedDataUtils;
import com.lyhengtongwl.zqsnews.utils.StringUtils;

import java.util.List;

/**
 * 我的订单
 */
public class MyorderAdapter extends BaseMultiItemQuickAdapter<MyorderEntity, BaseViewHolder> {

    Context mContext;
    String mType = "";//列表类型('订单类型：-1.全部；0.待支付；1.待收货；2.已完成；)

    public MyorderAdapter(@Nullable List<MyorderEntity> data, Context mContext, String type) {
        super(data);
        this.mContext = mContext;
        this.mType = type;
        try {
            //订单类型：0.普通；1.充值；2.竞拍；3.差价购买；原布局order_all_item
            addItemType(0, R.layout.item_myorder1);
            addItemType(1, R.layout.item_myorder1);
            addItemType(2, R.layout.item_myorder2);
            addItemType(3, R.layout.item_myorder3);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, MyorderEntity item) {
        //公共数据
        helper.setText(R.id.tv_title, item.getGoodsTitle())
                .setText(R.id.tv_time, StringUtils.timesToString(item.getAddTime().getTime()));

        Glide.with(mContext).load(item.getPreviewImage())
                .into((ImageView) helper.getView(R.id.iv_goodpic));

        //分类数据（价格）
        int type = helper.getItemViewType();
        switch (type) {
            case 0:
            case 1://普通和充值
                String t0p1 = "市场价：¥" + item.getDiscountPrice();
                String t0p2 = "使用购物币：" + item.getShoppingCurrency();
                String t0p3 = "实付价：¥" + item.getPaymentAmount();
                SpannableString ss0 = new SpannableString(t0p3);
                ForegroundColorSpan cs0 = new ForegroundColorSpan(Color.parseColor("#FF6600"));
                int pos0 = t0p3.indexOf("¥");
                ss0.setSpan(cs0, pos0, ss0.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                helper.setText(R.id.tv_price1, t0p1)
                        .setText(R.id.tv_price2, t0p2)
                        .setText(R.id.tv_price3, ss0);
                break;
            case 2://竞拍
                String t2p1 = "市场价：¥" + item.getOriginalPrice();
                String t2p2 = "实付价：¥" + item.getPaymentAmount();
                SpannableString ss2 = new SpannableString(t2p2);
                ForegroundColorSpan cs2 = new ForegroundColorSpan(Color.parseColor("#FF6600"));
                int pos2 = t2p2.indexOf("¥");
                ss2.setSpan(cs2, pos2, ss2.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                helper.setText(R.id.tv_price1, t2p1)
                        .setText(R.id.tv_price2, ss2)
                        .setText(R.id.tv_pricerate, FormattedDataUtils.decimalToPercentage(item.getDiscountRate()));
                break;
            case 3://差价购买
                String t3p1 = "市场价：¥" + item.getOriginalPrice();
                String t3p2 = "消耗幸运豆：" + item.getBiddingCurrency();
                float orgp1 = Float.valueOf(item.getOriginalPrice());
                float bidp2 = Float.valueOf(item.getBiddingCurrency());
                String t3p3;
                if (orgp1 >= bidp2) {
                    t3p3 = "实付价：¥" + (orgp1 - bidp2);
                } else {
                    t3p3 = "实付价：¥0";
                }
                SpannableString ss3 = new SpannableString(t3p3);
                ForegroundColorSpan cs3 = new ForegroundColorSpan(Color.parseColor("#FF6600"));
                int pos3 = t3p3.indexOf("¥");
                ss3.setSpan(cs3, pos3, ss3.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                helper.setText(R.id.tv_price1, t3p1)
                        .setText(R.id.tv_price2, t3p2)
                        .setText(R.id.tv_price3, ss3);
                break;
        }

        //按钮
        switch (Integer.valueOf(mType)) {
            case -1://全部列表
                if ("-1".equals(item.getStatus())) {
                    helper.setGone(R.id.tv_btn, true)
                            .setText(R.id.tv_btn, "支付超时");
                } else if ("0".equals(item.getStatus())) {
                    helper.setGone(R.id.tv_btn, true)
                            .setText(R.id.tv_btn, "待支付")
                            .addOnClickListener(R.id.tv_btn);
                } else if ("1".equals(item.getStatus())) {
                    helper.setGone(R.id.tv_btn, true)
                            .setText(R.id.tv_btn, "支付中");
                } else if ("10".equals(item.getStatus())) {
                    helper.setGone(R.id.tv_btn, true)
                            .setText(R.id.tv_btn, "待发货");
                } else if ("20".equals(item.getStatus())) {
                    helper.setGone(R.id.tv_btn, true)
                            .setText(R.id.tv_btn, "待收货")
                            .addOnClickListener(R.id.tv_btn);
                } else if ("30".equals(item.getStatus())) {
                    helper.setGone(R.id.tv_btn, true)
                            .setText(R.id.tv_btn, "待评价")
                            .addOnClickListener(R.id.tv_btn);
                } else if ("40".equals(item.getStatus())) {
                    helper.setGone(R.id.tv_btn, true)
                            .setText(R.id.tv_btn, "已完成");
                }
                break;
            case 0://待支付
                if ("0".equals(item.getStatus())) {
                    helper.setGone(R.id.tv_btn, true)
                            .setText(R.id.tv_btn, "立即支付")
                            .addOnClickListener(R.id.tv_btn);
                } else {
                    helper.setGone(R.id.tv_btn, false);
                }
                break;
            case 1://待收货
                if ("20".equals(item.getStatus())) {
                    helper.setGone(R.id.tv_btn, true)
                            .setText(R.id.tv_btn, "签收")
                            .addOnClickListener(R.id.tv_btn);
                } else {
                    helper.setGone(R.id.tv_btn, false);
                }
                break;
            case 2://已完成
                if ("30".equals(item.getStatus())) {
                    helper.setGone(R.id.tv_btn, true)
                            .setText(R.id.tv_btn, "晒单")
                            .addOnClickListener(R.id.tv_btn);
                } else {
                    helper.setGone(R.id.tv_btn, false);
                }
                break;
        }

    }
}
